package com.guangzhi.weijianzhi.company.manager;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.Map;
import java.util.Set;

import android.annotation.SuppressLint;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.AdapterView.OnItemClickListener;

import com.guangzhi.weijianzhi.R;
import com.guangzhi.weijianzhi.activity.BaseActivity;
import com.guangzhi.weijianzhi.adapter.MyAddressadapterOne;
import com.guangzhi.weijianzhi.adapter.MyAddressadapterThree;
import com.guangzhi.weijianzhi.adapter.MyAddressadapterTwo;
import com.guangzhi.weijianzhi.adapter.MyArrayApterOne;
import com.guangzhi.weijianzhi.company.center.CompileCompanyInfoActivity;
import com.guangzhi.weijianzhi.company.manager.PublishPositionThreeActivity.CloseReceiver;
import com.guangzhi.weijianzhi.entity.BeansCity;
import com.guangzhi.weijianzhi.entity.BeansDistrict;
import com.guangzhi.weijianzhi.entity.BeansProvince;
import com.guangzhi.weijianzhi.entity.PublishPositionEntity;
import com.guangzhi.weijianzhi.utils.Misc;
import com.guangzhi.weijianzhi.utils.PopuUtils;
import com.guangzhi.weijianzhi.utils.ProvinceCity;
import com.guangzhi.weijianzhi.view.IndustryData;
import com.guangzhi.weijianzhi.view.IndustryData.ProvinceAndCity;

public class PublishPositionTwoActivity extends BaseActivity implements OnClickListener {

	private TextView posi_data;
	private TextView posi_time;
	
	private ListView addreTwo_list;
	private ListView addreThree_list;
	private ListView addressOne_list;
	private String name="";
	private String Cityname="";
	private ArrayList<BeansCity> citys;
	private MyAddressadapterTwo MyAddressadapterTwo ;
	private ArrayList<BeansDistrict> districts;
	private Map<String, List<ProvinceAndCity>> initData;
	private ArrayList<BeansProvince> privinces = null;
	private TextView posi_area;
	private String areaCode;
	private EditText mNumbert;
	private TextView pay_type;
	private EditText address;
	private EditText pay_number;
	private String bouns_type ="1";
	 private CloseReceiver closeReceiver;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.tz_publish_positon_two_layout);
		 initData = IndustryData.initData(getResources());
		 privinces = ProvinceCity.initData(PublishPositionTwoActivity.this);
		initView();
		onregistBroab();
	}
	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		unregisterReceiver(closeReceiver);
	}
	
	private void onregistBroab() {
		// TODO Auto-generated method stub
		closeReceiver = new CloseReceiver();  
		IntentFilter intentFilter = new IntentFilter("com.guangzhi.weijianzhi");  
		registerReceiver(closeReceiver, intentFilter);  
	}
	   /** 
	    * 结束Activity的广播接收机 
	    * @author shuxiang 
	    * 
	    */  
	   public class CloseReceiver extends BroadcastReceiver  
	{   
	       @Override  
	       public void onReceive(Context context, Intent intent)  
	       {  
	    	   PublishPositionTwoActivity.this.finish();  
	       }  
	   } 
	private void initView() {
		
		initTitle();
		goBack.setOnClickListener(this);
		titleTextV.setText("发布职位");
		mNumbert = (EditText) findViewById(R.id.posi_number);
		address = (EditText) findViewById(R.id.address);
		pay_type = (TextView) findViewById(R.id.pay_type);
		posi_data = (TextView) findViewById(R.id.posi_data);
		posi_time = (TextView) findViewById(R.id.posi_time);
		posi_area = (TextView) findViewById(R.id.posi_area);
		pay_number = (EditText) findViewById(R.id.pay_number);
		
		findViewById(R.id.next).setOnClickListener(this);
		pay_type.setOnClickListener(this);
		findViewById(R.id.posi_type).setOnClickListener(this);
		findViewById(R.id.base_time).setOnClickListener(this);
		findViewById(R.id.base_area).setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.pay_type://支付类型
			showPayType();
			break;
		case R.id.posi_type://工作日期posi_type
			showPosiData();
			break;
		case R.id.base_time://工作时间
			showPosiTime();
			break;
		case R.id.base_area://地址省市区
			showAddress();
			break;

		case R.id.next:
			if(isNotSelect()){
				PublishPositionEntity.posiNumber =mNumbert.getText().toString();
				PublishPositionEntity.posiPay =pay_number.getText().toString();
				PublishPositionEntity.bouns_type =bouns_type;
				PublishPositionEntity.posiData =posi_data.getText().toString();
				PublishPositionEntity.positime =posi_time.getText().toString();
				PublishPositionEntity.posiArea =areaCode;
				PublishPositionEntity.posiAddress =address.getText().toString();
				Intent intent = new Intent(this, PublishPositionThreeActivity.class);
				startActivity(intent);
			}
		break;
		case R.id.title_back:
			onBackPressed();
			break;
		}
	}
	
	
	private void showPayType() {
		// TODO Auto-generated method stub

		// TODO Auto-generated method stub
		final ArrayList<String> arrays = new ArrayList<String>();
		arrays.add("元/天");arrays.add("元/小时");
		
		// TODO Auto-generated method stub
		View view = View.inflate(this, R.layout.tz_pop_list2_layout, null);
		ListView list = (ListView) view.findViewById(R.id.one_list);
		final PopupWindow showPou = PopuUtils.showPou(View.inflate(this, R.layout.tz_company_info_layout, null), view, this, this);
		MyArrayApterOne array = new MyArrayApterOne(arrays, this);
		list.setAdapter(Misc.getMyadapter(array, list));
		list.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				showPou.dismiss();
				pay_type.setText(arrays.get(arg2));
				
				bouns_type=(++arg2)+"";
			}
		});
	
	}

	private boolean isNotSelect(){
		
		
		if(!Misc.notNull(mNumbert.getText().toString())){
			Misc.alert("请输入招聘人数");
			return false;
		}else if(!Misc.notNull(pay_number.getText().toString())){
			Misc.alert("请填写薪资待遇");
			return false;
		}/*else if(Integer.parseInt(pay_number.getText().toString())<5 && "元/小时".equals(pay_type.getText().toString())){
			Misc.alert("最低5元/小时");
			return false;
		}else if(Integer.parseInt(pay_number.getText().toString())<20 && "元/天".equals(pay_type.getText().toString())){
			Misc.alert("最低20元/天");
			return false;
		}
		*/else if(!Misc.notNull(posi_data.getText().toString())){
			Misc.alert("请选择工作日期");
			return false;
		}else if(!Misc.notNull(posi_time.getText().toString())){
			Misc.alert("请选择工作时间");
			return false;
		}else if(!Misc.notNull(posi_area.getText().toString())){
			Misc.alert("请选择工作地点");
			return false;
		}else if(!Misc.notNull(address.getText().toString())){
			Misc.alert("请输入详细地址");
			return false;
		}else{
			return true;
		}
		
		
	}
	

	private void showPosiTime() {
		// TODO Auto-generated method stub
		View view = View.inflate(this, R.layout.tz_double_timepicker_layout, null);
		final PopupWindow showPou = PopuUtils.showPou(View.inflate(this, R.layout.tz_company_info_layout, null), view, this, this);
		final TimePicker timePicker = (TimePicker) view.findViewById(R.id.timePickero);
		final TimePicker timePicker2 = (TimePicker) view.findViewById(R.id.tiemPickertwot);
		timePicker2.setVisibility(View.GONE);
		timePicker.setIs24HourView(true);
		timePicker2.setIs24HourView(true);
		timePicker.setCurrentHour(8);
		timePicker.setCurrentMinute(00);
		timePicker2.setCurrentHour(8);
		timePicker2.setCurrentMinute(00);
		final Button mBack = (Button) view.findViewById(R.id.title_back);
		final TextView start_tv = (TextView) view.findViewById(R.id.start_tv);
		final TextView stop_tv = (TextView) view.findViewById(R.id.stop_tv);
		TextView tv = (TextView) view.findViewById(R.id.title_lable);
		tv.setText("时间选择");
		stop_tv.setVisibility(View.GONE);
		mBack.setVisibility(View.GONE);
		
		final Button right = (Button) view.findViewById(R.id.title_right);
		right.setVisibility(View.VISIBLE);
		right.setText("确定");
		final Button right2 = (Button) view.findViewById(R.id.title_right2);
		right2.setVisibility(View.GONE);
		right2.setText("完成");
		
          mBack.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				start_tv.setVisibility(View.VISIBLE);
				timePicker.setVisibility(View.VISIBLE);
				timePicker2.setVisibility(View.GONE);
				right.setVisibility(View.VISIBLE);
				right2.setVisibility(View.GONE);
				mBack.setVisibility(View.GONE);
				stop_tv.setVisibility(View.GONE);
			}
		});
		
		
		right.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				
				start_tv.setVisibility(View.GONE);
				timePicker.setVisibility(View.GONE);
				timePicker2.setVisibility(View.VISIBLE);
				right.setVisibility(View.GONE);
				right2.setVisibility(View.VISIBLE);
				mBack.setVisibility(View.VISIBLE);
				stop_tv.setVisibility(View.VISIBLE);
				
			}
		});
		
		
		
		right2.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				int currentHour = timePicker.getCurrentHour();
				    int currentMinute = timePicker.getCurrentMinute();
				    int currentHour2 = timePicker2.getCurrentHour();
				    int currentMinute2 = timePicker2.getCurrentMinute();
				    
				   if(isTimeOk(currentHour,currentMinute,currentHour2,currentMinute2)){
					   String newcurrentMinute;
					   String newcurrentMinute2;
					   if(currentMinute<10){
						 newcurrentMinute ="0"+currentMinute;
					   }else {
						   newcurrentMinute =""+currentMinute;
					   }
					   if(currentMinute2<10){
						   newcurrentMinute2 ="0"+currentMinute2;
					   }else {
						   newcurrentMinute2 =""+currentMinute2;
					   }
					   posi_time.setText(currentHour+":"+newcurrentMinute+"-"+currentHour2+":"+newcurrentMinute2);
					   showPou.dismiss();
				   }else{
					   Misc.alert("请选择正确的时间");
				   }
			}
		});
	}

	private boolean isTimeOk(int currentHour, int currentMinute,
			int currentHour2, int currentMinute2) {
		// TODO Auto-generated method stub
		if(currentHour2>currentHour){
			return true;
		}else{
			if(currentHour2==currentHour && currentMinute2>currentMinute){
				return true;
			}else{
				return false;
			}
		}
	}

	@SuppressLint("NewApi")
	private void showPosiData() {
		// TODO Auto-generated method stub
		View view = View.inflate(this, R.layout.tz_doube_picker, null);
		final PopupWindow showPou = PopuUtils.showPou(View.inflate(this, R.layout.tz_company_info_layout, null), view, this, this);
		final Button mBack = (Button) view.findViewById(R.id.title_back);
		final TextView start_tv = (TextView) view.findViewById(R.id.start_tv);
		final TextView stop_tv = (TextView) view.findViewById(R.id.stop_tv);
		TextView tv = (TextView) view.findViewById(R.id.title_lable);
		tv.setText("日期选择");
		stop_tv.setVisibility(View.GONE);
		mBack.setVisibility(View.GONE);
		
		
		
		  Calendar cal = Calendar.getInstance();
		final DatePicker datePicker = (DatePicker) view.findViewById(R.id.datePicker);
		final DatePicker datePickertwo = (DatePicker) view.findViewById(R.id.datePickertwo);
		datePickertwo.setVisibility(View.GONE);
		final Button right = (Button) view.findViewById(R.id.title_right);
		right.setVisibility(View.VISIBLE);
		right.setText("确定");
		final Button right2 = (Button) view.findViewById(R.id.title_right2);
		right2.setVisibility(View.GONE);
		right2.setText("完成");
		
		
		mBack.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				start_tv.setVisibility(View.VISIBLE);
				datePicker.setVisibility(View.VISIBLE);
				datePickertwo.setVisibility(View.GONE);
				right.setVisibility(View.VISIBLE);
				right2.setVisibility(View.GONE);
				mBack.setVisibility(View.GONE);
				stop_tv.setVisibility(View.GONE);
			}
		});
		
		
		right.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				
				start_tv.setVisibility(View.GONE);
				datePicker.setVisibility(View.GONE);
				datePickertwo.setVisibility(View.VISIBLE);
				right.setVisibility(View.GONE);
				right2.setVisibility(View.VISIBLE);
				mBack.setVisibility(View.VISIBLE);
				stop_tv.setVisibility(View.VISIBLE);
				
			}
		});
		right2.findViewById(R.id.title_right2).setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				int year = datePicker.getYear();
				int month = datePicker.getMonth();
				int dayOfMonth = datePicker.getDayOfMonth();
				
				int year2 = datePickertwo.getYear();
				int month2 = datePickertwo.getMonth();
				int dayOfMonth2 = datePickertwo.getDayOfMonth();
				if(isOk(year,month,dayOfMonth,year2,month2,dayOfMonth2)){
					posi_data.setText(year+"-"+(month+1)+"-"+dayOfMonth+"|"+year2+"-"+(month2+1)+"-"+dayOfMonth2);
					showPou.dismiss();
				}else{
					Misc.alert("请选择正确的日期");
				}
			}

			
		});
		 cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
		 datePicker.setMinDate(cal.getTimeInMillis());
		 datePickertwo.setMinDate(cal.getTimeInMillis());
		
		
	}
	private boolean isOk(int year, int month, int dayOfMonth, int year2,
			int month2, int dayOfMonth2) {
		// TODO Auto-generated method stub
		if(year2>year){
			return true;
		}else{
			if(month2>month){
				return true;
			}else{
				if(month2==month && dayOfMonth<=dayOfMonth2){
					return true;
				}else{
					return false;
				}
			}
		}
	}
	
    /**
     * 城市三级
     */
	private void showAddress() {
		// TODO Auto-generated method stub
		 View view	=View.inflate(this, R.layout.tz_three_listview_layout, null);
		 final PopupWindow window = new PopupWindow(view, LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT,true);
			PopuUtils.addAttriBute(window);
			window.showAtLocation(View.inflate(this, R.layout.tz_company_info_layout,null), Gravity.BOTTOM, 0, 0);
			PopuUtils.changeActivity(PublishPositionTwoActivity.this);
			PopuUtils.changeActivity(window,PublishPositionTwoActivity.this);
		 
		 
			addressOne_list = (ListView)view. findViewById(R.id.one_list);
			final MyAddressadapterOne MyAddressadapterOne = new MyAddressadapterOne(privinces,PublishPositionTwoActivity.this);
			addressOne_list.setAdapter(Misc.getMyadapter(MyAddressadapterOne, addressOne_list));
			addressOne_list.setOnItemClickListener(new OnItemClickListener() {
				@Override
				public void onItemClick(AdapterView<?> parent, View view,
						int position, long id) {
					// TODO Auto-generated method stub
					MyAddressadapterOne.isSelect(position);
					citys = privinces.get(position).getCitys();
					MyAddressadapterTwo = new MyAddressadapterTwo(citys,PublishPositionTwoActivity.this);
					name  = privinces.get(position).getName();
					addreTwo_list.setAdapter(Misc.getMyadapter(MyAddressadapterTwo, addreTwo_list));
					
					if(addreThree_list.getVisibility()==View.VISIBLE){
						addreThree_list.setVisibility(View.GONE);
					}
					
				}
			});
			addreTwo_list = (ListView) view.findViewById(R.id.two_list);
			addreThree_list = (ListView) view.findViewById(R.id.three_list);
			addreTwo_list.setOnItemClickListener(new OnItemClickListener() {
				@Override
				public void onItemClick(AdapterView<?> parent, View view,
						int position, long id) {
					// TODO Auto-generated method stub
					MyAddressadapterTwo.isSelect(position);
					  districts = citys.get(position).getDistricts();
					  Cityname =  citys.get(position).getName();
					 addreThree_list.setAdapter(Misc.getMyadapter(new MyAddressadapterThree(districts,PublishPositionTwoActivity.this), addreThree_list));
					 if(addreThree_list.getVisibility()==View.GONE){
						 addreThree_list.setVisibility(View.VISIBLE);
					 }
				}
			});
			addreThree_list.setOnItemClickListener(new OnItemClickListener() {

				@Override
				public void onItemClick(AdapterView<?> parent, View view,
						int position, long id) {
					// TODO Auto-generated method stub
					String str ="";
					BeansDistrict beansDistrict = districts.get(position);
					
					if(name.equals(Cityname)){
						str =name+"/"+beansDistrict.getName();
					}else{
						
						str =name+"/"+Cityname+"/"+beansDistrict.getName();
					}
					areaCode =beansDistrict.getId();
			//		String id_name = beansDistrict.getId()+"|"+str;
					posi_area.setText(str);
					window.dismiss();
				}
			});
	}
	
}
